var EVENT_MAP = {
  k_8: KeyEvent.BACK_KEY, //返回
  k_13: KeyEvent.OK_KEY, //ok
  k_38: KeyEvent.UP_KEY, //上
  k_37: KeyEvent.LEFT_KEY, //左
  k_39: KeyEvent.RIGHT_KEY, //右
  k_40: KeyEvent.DOWN_KEY, //下
  k_33: KeyEvent.PAGEUP_KEY, //上一页
  k_34: KeyEvent.PAGEDOWN_KEY, //下一页

  k_48: KeyEvent.ZERO_KEY, //0
  k_49: KeyEvent.ONE_KEY, //1
  k_50: KeyEvent.TWO_KEY, //2
  k_51: KeyEvent.THREE_KEY, //3
  k_52: KeyEvent.FOUR_KEY, //4
  k_53: KeyEvent.FIVE_KEY, //5
  k_54: KeyEvent.SIX_KEY, //6
  k_55: KeyEvent.SEVEN_KEY, //7
  k_56: KeyEvent.EIGHT_KEY, //8
  k_57: KeyEvent.NINE_KEY, //9
  k_105: KeyEvent.POUND_KEY, //#,输入法切换
  k_106: KeyEvent.STAR_KEY, //*

  k_264: KeyEvent.FFWD_KEY, //快进
  k_265: KeyEvent.FBWD_KEY, //快退
  k_257: KeyEvent.CHANNELUP_KEY, //频道加
  k_258: KeyEvent.CHANNELDOWN_KEY, //频道减
  k_259: KeyEvent.VOLUMEUP_KEY, //音量加
  k_260: KeyEvent.VOLUMEDOWN_KEY, //音量减
  k_261: KeyEvent.SILENT_KEY, //静音
  k_262: KeyEvent.AUDIOTRACK_KEY, //音轨
  k_263: KeyEvent.PAUSEPLAY_KEY, //>||，播放，暂停
  k_270: KeyEvent.STOP_KEY, //停止
  k_272: KeyEvent.PORTAL_KEY, //首页
  k_286: KeyEvent.AUDIOCHANNEL_KEY, //声道
  k_560: KeyEvent.SUBTITLE_KEY, //字幕

  k_275: KeyEvent.RED_KEY, //快捷键（红色）
  k_276: KeyEvent.GREEN_KEY, //快捷键（绿色）
  k_277: KeyEvent.YELLOW_KEY, //快捷键（黄色）
  k_278: KeyEvent.BLUE_KEY, //快捷键（蓝色）
  k_271: KeyEvent.POSITION_KEY, //定位
  k_42: KeyEvent.POSITION_KEY, //定位 兼容
  k_281: KeyEvent.FAVORITE_KEY, //收藏夹
  //	"k_284": KeyEvent.HELP_KEY,  //帮助
  k_515: KeyEvent.HELP_KEY, //帮助
  k_768: STBEvent.KEY_IPTV_EVENT, //虚拟事件

  k_268: KeyEvent.INFO_KEY, //信息
  k_1105: KeyEvent.SEARCH_KEY, //搜索

  k_1108: KeyEvent.BTV_KEY, //直播
  k_1109: KeyEvent.VOD_KEY, //点播
  k_1110: KeyEvent.TVOD_KEY, //回看
  k_1111: KeyEvent.COMM_KEY, //通信
  k_1112: KeyEvent.NVOD_KEY, //轮播
  k_283: KeyEvent.UNDERLINE_KEY, //下划线
  k_112: KeyEvent.F1_KEY, //F1
  k_113: KeyEvent.F2_KEY, //F2
  k_114: KeyEvent.F3_KEY, //F3
  k_115: KeyEvent.F4_KEY, //F4
  //"k_282": KeyEvent.BOOKMARK_KEY,//书签
  k_1377: KeyEvent.VOICE_KEY,

  k_280: KeyEvent.KEYLAST_KEY, //快速切换
  k_1120: KeyEvent.OPTION_KEY, //OPTION键
  k_132: KeyEvent.PIP_KEY, //画中画
  k_1378: KeyEvent.MULTIVIEW_KEY, //四屏同看
};

document.onirkeydown = grabEvent;

document.onkeydown = grabEvent;


function grabEvent(event) {
  var keyCode = event.which || event.keyCode;
  var resultEvent = EVENT_MAP["k_" + keyCode];
  switch (resultEvent) {
    case KeyEvent.ZERO_KEY:
      typeof keyZore ==  'function' && keyZore();
      break;
    case KeyEvent.ONE_KEY:
      typeof keyOne ==  'function' && keyOne();
      break;
    case KeyEvent.TWO_KEY:
      typeof keyTwo ==  'function' && keyTwo();
      break;
    case KeyEvent.THREE_KEY:
      typeof keyThree ==  'function' && keyThree();
      break;
    case KeyEvent.FOUR_KEY:
      typeof keyFour ==  'function' && keyFour();
      break;
    case KeyEvent.FIVE_KEY:
      typeof keyFive ==  'function' && keyFive();
      break;
    case KeyEvent.SIX_KEY:
      typeof keySix ==  'function' && keySix();
      break;
    case KeyEvent.SEVEN_KEY:
      typeof keySeven ==  'function' && keySeven();
      break;
    case KeyEvent.EIGHT_KEY:
      typeof keyEight ==  'function' && keyEight();
      break;
    case KeyEvent.NINE_KEY:
      typeof keyNine ==  'function' && keyNine();
      break;
    case KeyEvent.BACK_KEY:
      typeof keyBack == 'function' && keyBack()
      break;
    case KeyEvent.OK_KEY:
      typeof keyEnter == 'function' && keyEnter()
      break;
    case KeyEvent.UP_KEY:
      typeof keyUp == 'function' && keyUp()
      break;
    case KeyEvent.LEFT_KEY:
      typeof keyLeft == 'function' && keyLeft()
      break;
    case KeyEvent.RIGHT_KEY:
      typeof keyRight == 'function' && keyRight()
      break;
    case KeyEvent.DOWN_KEY:
      typeof keyDown == 'function' && keyDown()
      break;
    case KeyEvent.VOLUMEUP_KEY:
      typeof keyVolumeUp == 'function' && keyVolumeUp()
      break;
    case KeyEvent.VOLUMEDOWN_KEY:
      typeof keyVolumeDown == 'function' && keyVolumeDown()
      break;
    case KeyEvent.SILENT_KEY:
      typeof keyMute == 'function' && keyMute()
      break;
    case STBEvent.KEY_IPTV_EVENT:
      eval("eventJson=" + Utility.getEvent());
      if (eventJson.type == "EVENT_MEDIA_END") {
        typeof video === 'object' && typeof video.goUtility === 'function' && video.goUtility()
      } else if ("EVENT_PLAYMODE_CHANGE" == eventJson.type) {
        
      }
      break;
    default:
      switch (keyCode) {
        case 48:
          typeof keyMute == 'function' && keyMute();
          break;
        case 187:
          typeof keyVolumeUp == 'function' && keyVolumeUp();
          break;
        case 189:
          typeof keyVolumeDown == 'function' && keyVolumeDown();
          break;
        case 96:
          typeof keyZore ==  'function' && keyZore();
          break;
        case 97:
          typeof keyOne ==  'function' && keyOne();
          break;
        case 98:
          typeof keyTwo ==  'function' && keyTwo();
          break;
        case 99:
          typeof keyThree ==  'function' && keyThree();
          break;
        case 100:
          typeof keyFour ==  'function' && keyFour();
          break;
        case 101:
          typeof keyFive ==  'function' && keyFive();
          break;
        case 102:
          typeof keySix ==  'function' && keySix();
          break;
        case 103:
          typeof keySeven ==  'function' && keySeven();
          break;
        case 104:
          typeof keyEight ==  'function' && keyEight();
          break;
        case 105:
          typeof keyNine ==  'function' && keyNine();
          break;

      }
      break;
  }
}

var num = 0;
var oldTime = new Date().getTime();

function keyZore() {
    var newTime = new Date().getTime();
    if (num == 0) {
        num++;
        oldTime = newTime;
    } else {
        if (newTime - oldTime < 1000) {
            num++;
            oldTime = newTime;
            if (num === 4) {
                num = 0;
                var url = window.location.href;
                var return_index = url.indexOf('ReturnURL=');
                var returnUrl = "";
                if (return_index > 0) {
                    // 第一次进SP，添加domain
                    returnUrl = url.substring(return_index + 10);
                }
                window.location.href = "http://192.168.129.132:12201/litchi/index.html?ReturnURL=" + returnUrl;
            }
        } else {
            num = 1;
            oldTime = newTime;
        }
    }
}
